﻿package njClass.UI
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PlusCheckBox extends Sprite
	{
		private var BoxWidth:Number;
		private var BoxHeight:Number;
		private var values:Array;
		private var currentID:int=0;
		private	var checkbox:Sprite=new Sprite();
		private	var up_mc:Sprite=new Sprite();
		private var over_mc:Sprite=new Sprite();
		private var ValueKind:Boolean;
		private var used:Boolean=true;
		
		public function PlusCheckBox(w:Number,h:Number,useValue:Boolean=false,Values:Array=null)
		{
			BoxWidth=w;
			BoxHeight=h;
			ValueKind=useValue;
			if(!ValueKind){
				values=new Array(false,true);
			}else{
				values=Values;
			}
			up_mc.addChild(doDraw(w,h,0xCCCCCC));
			checkbox.addChildAt(up_mc,0);
			over_mc.addChild(doDraw(w,h,0xCCCCCC,0xFF3300));
			addChild(checkbox);
			checkbox.addEventListener(MouseEvent.CLICK,clickTheBox);
			checkbox.addEventListener(MouseEvent.MOUSE_OVER,overTheBox);
		}
		
		public function set enabled(res:Boolean):void{
			checkbox.removeEventListener(MouseEvent.CLICK,clickTheBox);
			checkbox.removeEventListener(MouseEvent.MOUSE_OVER,overTheBox);
			used=res;
			if(used){
				checkbox.alpha=1;
				checkbox.addEventListener(MouseEvent.CLICK,clickTheBox);
				checkbox.addEventListener(MouseEvent.MOUSE_OVER,overTheBox);
			}else{
				checkbox.alpha=0.5;
				checkbox.removeEventListener(MouseEvent.CLICK,clickTheBox);
				checkbox.removeEventListener(MouseEvent.MOUSE_OVER,overTheBox);
			}
		}
		
		public function get value():*{
			return values[currentID];
		}
		
		public function set value(num:*):void{
			//trace(num);
			currentID=values.indexOf(num);
			UpAct();
		}
		
		private function overTheBox(e:MouseEvent):void{
			checkbox.removeEventListener(MouseEvent.MOUSE_OVER,overTheBox);
			checkbox.addEventListener(MouseEvent.MOUSE_OUT,outTheBox);
			checkbox.removeChild(up_mc);
			checkbox.addChildAt(over_mc,0);
			
			function outTheBox(e:MouseEvent):void{
				checkbox.addEventListener(MouseEvent.MOUSE_OVER,overTheBox);
				checkbox.removeEventListener(MouseEvent.MOUSE_OUT,outTheBox);
				checkbox.addChildAt(up_mc,0);
				checkbox.removeChild(over_mc);
			}
		}
		
		private function clickTheBox(e:MouseEvent):void{
			currentID=(currentID==0)?1:0;
			UpAct();
			dispatchEvent(new Event("SliderChange"));
		}
		
		private function UpAct():void{
			if(checkbox.getChildByName("kick")!=null){
				checkbox.removeChild(checkbox.getChildByName("kick"));
			}
			if(currentID==1){
				var kick:Sprite=new Sprite();
				kick.name="kick";
				kick.addChild(drawTick.createTick(BoxWidth,BoxHeight));
				checkbox.addChild(kick);
			}
		}
		
		private function doDraw(w:Number,h:Number,bgColor:uint,lineColor:uint=0x000000):Shape{
			var temp:Shape=new Shape();
			temp.graphics.beginFill(bgColor);
			temp.graphics.lineStyle(2,lineColor,0.6,true);
			temp.graphics.drawRoundRect(0,0,w,h,5);
			temp.graphics.endFill();
			return temp;
		}
	}
}